package com.chunyu.app.utils;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;

public class EqualsUtils {

	private static boolean innerEquals(Integer newValue, Integer oldValue) {
		return newValue.compareTo(oldValue) == 0;
	}

	private static boolean innerEquals(Long newValue, Long oldValue) {
		return newValue.compareTo(oldValue) == 0;
	}

	private static boolean innerEquals(Short newValue, Short oldValue) {
		return newValue.compareTo(oldValue) == 0;
	}

	private static boolean innerEquals(Float newValue, Float oldValue) {
		return newValue.compareTo(oldValue) == 0;
	}

	private static boolean innerEquals(Double newValue, Double oldValue) {
		return newValue.compareTo(oldValue) == 0;
	}

	private static boolean innerEquals(BigDecimal newValue, BigDecimal oldValue) {
		return newValue.compareTo(oldValue) == 0;
	}

	private static boolean innerEquals(Date newValue, Date oldValue) {
		return newValue.compareTo(oldValue) == 0;
	}

	private static boolean innerEquals(byte[] newValue, byte[] oldValue) {
		return new String(newValue).equals(new String(oldValue));
	}

	public static boolean objectEquals(Object newObj, Object oldObj) {
		Map<?, ?> oldMap = oldObj instanceof Map ? (Map<?, ?>) oldObj : BeanUtils.toMap(oldObj, false);
		Map<?, ?> newMap = newObj instanceof Map ? (Map<?, ?>) newObj : BeanUtils.toMap(newObj, false);
		for (Object key : newMap.keySet()) {
			if (!oldMap.containsKey(key)) {
				return false;
			} else if (!EqualsUtils.equals(newMap.get(key), oldMap.get(key))) {
				return false;
			}
		}
		return true;

	}

	/**
	 * 比较两个值是否相等
	 * 
	 * @param newValue
	 * @param oldValue
	 * @return
	 */
	public static boolean equals(Object newValue, Object oldValue) {
		if (NullUtils.isEmpty(newValue) && NullUtils.isEmpty(oldValue)) {
			return true;
		} else if (NullUtils.isEmpty(newValue) || NullUtils.isEmpty(oldValue)) {
			return false;
		} else if (newValue instanceof Map || oldValue instanceof Map) {
			return newValue.equals(oldValue);
		} else if (!newValue.getClass().getName().equals(oldValue.getClass().getName())) {
			return false;
		} else if (newValue instanceof Integer) {
			return innerEquals((Integer) (newValue), (Integer) oldValue);
		} else if (newValue instanceof Long) {
			return innerEquals((Long) (newValue), (Long) oldValue);
		} else if (newValue instanceof Short) {
			return innerEquals((Short) (newValue), (Short) oldValue);
		} else if (newValue instanceof Float) {
			return innerEquals((Float) (newValue), (Float) oldValue);
		} else if (newValue instanceof Double) {
			return innerEquals((Double) (newValue), (Double) oldValue);
		} else if (newValue instanceof BigDecimal) {
			return innerEquals((BigDecimal) (newValue), (BigDecimal) oldValue);
		} else if (newValue instanceof Date) {
			return innerEquals((Date) (newValue), (Date) oldValue);
		} else if (newValue instanceof byte[]) {
			return innerEquals((byte[]) (newValue), (byte[]) oldValue);
		} else if (newValue instanceof String) {
			return newValue.equals(oldValue);
		} else {
			return objectEquals(newValue, oldValue);
		}
	}

	public static void main(String args[]) {
	}
}
